//package com.lengxf.gateway.bak;
//
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.core.io.buffer.DataBufferUtils;
//import org.springframework.core.io.buffer.NettyDataBuffer;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
//import org.springframework.util.StringUtils;
//import org.springframework.web.server.ServerWebExchange;
//import org.springframework.web.util.UriUtils;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.StandardCharsets;
//import java.util.function.Function;
//
//
//public class BodyUtils {
//
//    public static final String LOCAL_CACHED_REQUEST_BODY = "cachedRequestBodyObject";
//
//    private BodyUtils() {
//        throw new UnsupportedOperationException("do not initialization Utils class");
//    }
//
//    public static String toRaw(DataBuffer body) {
//        byte[] bytes = new byte[body.readableByteCount()];
//        body.read(bytes);
//        DataBufferUtils.release(body);
//        return uriDecode(new String(bytes, StandardCharsets.UTF_8));
//    }
//
//
//    private static String uriDecode(String encodeValue) {
//        if (StringUtils.isEmpty(encodeValue)) {
//            return "";
//        }
//        return UriUtils.decode(encodeValue.replaceAll("%(?![0-9a-fA-F]{2})", "%25"), StandardCharsets.UTF_8);
//    }
//
//
//    public static <T> Mono<T> cacheRequestBody(ServerWebExchange exchange, Function<ServerHttpRequest, Mono<T>> function) {
//        return DataBufferUtils.join(exchange.getRequest().getBody()).map(dataBuffer -> {
//            if (dataBuffer.readableByteCount() > 0) {
//                exchange.getAttributes().put(LOCAL_CACHED_REQUEST_BODY, dataBuffer);
//            }
//
//            ServerHttpRequest decorator = new ServerHttpRequestDecorator(exchange.getRequest()) {
//                @Override
//                public Flux<DataBuffer> getBody() {
//                    return Mono.<DataBuffer>fromSupplier(() -> {
//                        NettyDataBuffer pdb = (NettyDataBuffer) dataBuffer;
//                        return pdb.factory().wrap(pdb.getNativeBuffer().retainedSlice());
//                    }).flux();
//                }
//            };
//            return decorator;
//        }).switchIfEmpty(Mono.just(exchange.getRequest())).flatMap(function);
//    }
//
//}
